package com.wzy.business.util

import android.content.Context
import android.graphics.Bitmap
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.Utils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.wzy.business.R
import java.io.File
import java.util.concurrent.ExecutionException

object GlideUtils {
    fun setImageUrl(iv: ImageView, url: String?) {
        Glide.with(iv.context).load(url)
            .apply(RequestOptions().centerCrop())
            .into(iv)
    }

    fun setImageUrl(iv: ImageView, url: String?, @DrawableRes res: Int) {
        val options = RequestOptions().centerCrop()
            .placeholder(res)
        Glide.with(iv.context).load(url)
            .apply(options)
            .into(iv)
    }

    fun setImageUrl1(iv: ImageView, url: String?) {
        val options: RequestOptions = RequestOptions().centerCrop()
            .placeholder(R.color.color_F1F1F1)
        Glide.with(iv.context).load(url)
            .apply(options)
            .into(iv)
    }

    fun setImageUrl12(iv: ImageView, url: String?) {
        val options: RequestOptions = RequestOptions().centerInside()
            .placeholder(R.color.color_F1F1F1)
        Glide.with(iv.context).load(url)
            .apply(options)
            .into(iv)
    }

    fun setImageUrl(iv: ImageView, file: File?) {
        Glide.with(iv.context).load(file)
            .into(iv)
    }

    fun setImageUrl(iv: ImageView, file: File?, corner: Float) {
        Glide.with(iv.context).load(file)
            .apply(
                RequestOptions().transforms(
                    CenterCrop(),
                    RoundedCorners(ConvertUtils.dp2px(corner))
                )
            )
            .into(iv)
    }

    fun setImageUrl(iv: ImageView?, url: String?, corner: Float) {
        Glide.with(Utils.getApp()).load(url)
            .apply(
                RequestOptions().transforms(
                    CenterCrop(),
                    RoundedCorners(ConvertUtils.dp2px(corner))
                )
            )
            .into(iv!!)
    }

    fun getImage(context: Context?, url: String?): Bitmap? {
        return try {
            Glide.with(context!!).asBitmap()
                .load(url)
                .submit().get()
        } catch (e: ExecutionException) {
            e.printStackTrace()
            null
        } catch (e: InterruptedException) {
            e.printStackTrace()
            null
        }
    }
}
